/*
* Copyright (c) 2008-2009, Genox Software Ltd.
* All rights reserved.
*
* Author : Toby Riley - 2009
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Genox Software Ltd. nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Genox Software Ltd. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Genox Software Ltd. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.ReportingServices.Interfaces;
using System.Security.Permissions;
using System.Xml;
using System.Globalization;
using System.Threading;

namespace Secure.FTPDeliveryExtension
{
[Microsoft.ReportingServices.Diagnostics.ExtensionLocalizedName(typeof(FTPStrings), "ReportServerSecureFTPDeliveryProvider"), 
    StrongNameIdentityPermission(SecurityAction.LinkDemand, PublicKey=
        "00240000048000009400000006020000002400005253413100040000010001002f4d2e90a3573e"+
        "6e5c5cedc56ce60a6645172c7221e90a032cbf246f16cd28cd843fd4ddd892c4faab2dfe54d1e4"+
        "aaba2ad14c206c653d4e282da9184d8a7fa21e35f9f7d06128cc8c298e6ff6ccb2a3dc179b0518"+
        "924df992896e18cff246b01ed56602c6c609ce53b1bec20cc5d52999cd3d9aeabe056351f68fbc"+
        "20bb54c5")]
public sealed class FTPDeliveryUIControl : WebControl, ISubscriptionBaseUIUserControl, IExtension
{
    // Fields
    private CheckBox _cbIsDeliveryEncrypted = new CheckBox();
    private string _defaultRenderingExtension;
    private bool _isPriviledgedUser;
    private Label _lbCSVTextDelimiter = new Label();
    private Label _lbCSVTextQualifier = new Label();
    private Label _lbFileNameFormat = new Label();
    private Label _lbFTPFolder = new Label();
    private Label _lbFTPPassword = new Label();
    private Label _lbFTPPort = new Label();
    private Label _lbFTPUrl = new Label();
    private Label _lbFTPUsername = new Label();
    private Label _lbIsDeliveryEncrypted = new Label();
    private Label _lbPGPPublicKeyUID = new Label();
    private IDeliveryReportServerInformation _reportServerInformation;
    private TextBox _tbCSVTextDelimiter = new TextBox();
    private RequiredFieldValidator _tbCSVTextDelimiterRequiredFieldValidator = new RequiredFieldValidator();
    private TextBox _tbCSVTextQualifier = new TextBox();
    private RequiredFieldValidator _tbCSVTextQualifierRequiredFieldValidator = new RequiredFieldValidator();
    private TextBox _tbFileNameFormat = new TextBox();
    private TextBox _tbFTPFolder = new TextBox();
    private RequiredFieldValidator _tbFTPFolderRequiredFieldValidator = new RequiredFieldValidator();
    private TextBox _tbFTPPassword = new TextBox();
    private RequiredFieldValidator _tbFTPPasswordRequiredFieldValidator = new RequiredFieldValidator();
    private TextBox _tbFTPPort = new TextBox();
    private TextBox _tbFTPUrl = new TextBox();
    private RequiredFieldValidator _tbFTPUrlRequiredFieldValidator = new RequiredFieldValidator();
    private TextBox _tbFTPUsername = new TextBox();
    private RequiredFieldValidator _tbFTPUsernameRequiredFieldValidator = new RequiredFieldValidator();
    private TextBox _tbPGPPublicKeyUID = new TextBox();

    // Methods
    public FTPDeliveryUIControl()
    {
        base.Init += new EventHandler(FTPDeliveryUIProvider_Init);
    }

    private HtmlTableCell CreateHtmlTableCell()
    {
        HtmlTableCell cell = new HtmlTableCell();
        cell.NoWrap = true;
        return cell;
    }

    private HtmlTableCell CreateHtmlTableCell(int colspan)
    {
        HtmlTableCell cell = new HtmlTableCell();
        cell.NoWrap = true;
        cell.ColSpan = colspan;
        return cell;
    }

    private Control ErrorMessage(string error, bool noWrap)
    {
        string str = Page.Request.ApplicationPath + "/images/line_err1.gif";
        HtmlImage child = new HtmlImage();
        child.Src = str;
        child.Alt = "Error";
        HtmlTable table = new HtmlTable();
        table.Rows.Add(new HtmlTableRow());
        HtmlTableCell cell = new HtmlTableCell();
        cell.VAlign = "middle";
        cell.Attributes.Add("class", "msrs-validationerror");
        cell.Controls.Add(child);
        table.Rows[0].Cells.Add(cell);
        cell = new HtmlTableCell();
        cell.NoWrap = noWrap;
        cell.VAlign = "middle";
        cell.Attributes.Add("class", "msrs-validationerror");
        cell.Controls.Add(new LiteralControl(HttpUtility.HtmlEncode(error)));
        table.Rows[0].Cells.Add(cell);
        return table;
    }

    private void FTPDeliveryUIProvider_Init(object sender, EventArgs e)
    {
        RenderUIControls();
    }

    private Extension[] GetRenderFormats(bool respectVisible)
    {
        Extension[] renderingExtension = ReportServerInformation.RenderingExtension;
        ArrayList list = new ArrayList();
        foreach (Extension extension in renderingExtension)
        {
            if (!respectVisible || extension.Visible)
            {
                list.Add(extension);
            }
        }
        return (Extension[]) list.ToArray(typeof(Extension));
    }

    private void RenderUIControls()
    {
        HtmlTable table = new HtmlTable();
        table.Width = "100%";
        table.CellPadding = 0;
        table.CellSpacing = 0;
        table.Attributes.Add("class", "msrs-normal");
        HtmlTableRow row = new HtmlTableRow();
        HtmlTableCell cell = CreateHtmlTableCell();
        cell.Width = "15%";
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        cell.Width = "25%";
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        cell.Width = "40%";
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        cell.Width = "100%";
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbFTPUrl.Text = "FTP Url: ";
        cell.Controls.Add(_lbFTPUrl);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbFTPUrl.ID = "tbFTPUrl";
        _tbFTPUrl.CssClass = "msrs-txtBox";
        _tbFTPUrl.Style.Add("width", "100%");
        cell.Controls.Add(_tbFTPUrl);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        cell.VAlign = "middle";
        cell.Attributes.Add("class", "msrs-validationerror");
        _tbFTPUrlRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        _tbFTPUrlRequiredFieldValidator.ControlToValidate = "tbFTPUrl";
        _tbFTPUrlRequiredFieldValidator.Controls.Add(ErrorMessage("FTP Url is required", true));
        cell.Controls.Add(_tbFTPUrlRequiredFieldValidator);
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbFTPFolder.Text = "FTP Folder: ";
        cell.Controls.Add(_lbFTPFolder);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbFTPFolder.ID = "tbFTPFolder";
        _tbFTPFolder.CssClass = "msrs-txtBox";
        _tbFTPFolder.Style.Add("width", "100%");
        cell.Controls.Add(_tbFTPFolder);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        _tbFTPFolderRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        _tbFTPFolderRequiredFieldValidator.ControlToValidate = "tbFTPFolder";
        _tbFTPFolderRequiredFieldValidator.Controls.Add(ErrorMessage("FTP Folder is required", true));
        cell.Controls.Add(_tbFTPFolderRequiredFieldValidator);
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbFTPUsername.Text = "FTP Username: ";
        cell.Controls.Add(_lbFTPUsername);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbFTPUsername.ID = "tbFTPUsername";
        _tbFTPUsername.CssClass = "msrs-txtBox";
        _tbFTPUsername.Style.Add("width", "100%");
        cell.Controls.Add(_tbFTPUsername);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        _tbFTPUsernameRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        _tbFTPUsernameRequiredFieldValidator.ControlToValidate = "tbFTPUsername";
        _tbFTPUsernameRequiredFieldValidator.Controls.Add(ErrorMessage("FTP Username is required", true));
        cell.Controls.Add(_tbFTPUsernameRequiredFieldValidator);
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbFTPPassword.Text = "FTP Password: ";
        cell.Controls.Add(_lbFTPPassword);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbFTPPassword.ID = "tbFTPPassword";
        _tbFTPPassword.CssClass = "msrs-txtBox";
        _tbFTPPassword.Style.Add("width", "100%");
        cell.Controls.Add(_tbFTPPassword);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        _tbFTPPasswordRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        _tbFTPPasswordRequiredFieldValidator.ControlToValidate = "tbFTPPassword";
        _tbFTPPasswordRequiredFieldValidator.Controls.Add(ErrorMessage("FTP Password is required", true));
        cell.Controls.Add(_tbFTPPasswordRequiredFieldValidator);
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbFTPPort.Text = "FTP Port: ";
        cell.Controls.Add(_lbFTPPort);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbFTPPort.ID = "tbFTPPort";
        _tbFTPPort.CssClass = "msrs-txtBox";
        _tbFTPPort.Style.Add("width", "100%");
        cell.Controls.Add(_tbFTPPort);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = null;
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbIsDeliveryEncrypted.Text = "Encrypt Delivery: ";
        cell.Controls.Add(_lbIsDeliveryEncrypted);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _cbIsDeliveryEncrypted.ID = "cbIsDeliveryEncrypted";
        cell.Controls.Add(_cbIsDeliveryEncrypted);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbPGPPublicKeyUID.Text = "PGP Public Key UID: ";
        cell.Controls.Add(_lbPGPPublicKeyUID);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbPGPPublicKeyUID.ID = "tbPGPPublicKeyUID";
        _tbPGPPublicKeyUID.CssClass = "msrs-txtBox";
        _tbPGPPublicKeyUID.Style.Add("width", "100%");
        cell.Controls.Add(_tbPGPPublicKeyUID);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbFileNameFormat.Text = "File Name Format: ";
        cell.Controls.Add(_lbFileNameFormat);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbFileNameFormat.ID = "tbFileNameFormat";
        _tbFileNameFormat.CssClass = "msrs-txtBox";
        _tbFileNameFormat.Style.Add("width", "100%");
        cell.Controls.Add(_tbFileNameFormat);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbCSVTextDelimiter.Text = "CSV Text Delimiter: ";
        cell.Controls.Add(_lbCSVTextDelimiter);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbCSVTextDelimiter.ID = "tbCSVTextDelimiter";
        _tbCSVTextDelimiter.CssClass = "msrs-txtBox";
        _tbCSVTextDelimiter.Style.Add("width", "100%");
        cell.Controls.Add(_tbCSVTextDelimiter);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        _tbCSVTextDelimiterRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        _tbCSVTextDelimiterRequiredFieldValidator.ControlToValidate = "tbCSVTextDelimiter";
        _tbCSVTextDelimiterRequiredFieldValidator.Controls.Add(ErrorMessage("CSV Text Delimiter is required", true));
        cell.Controls.Add(_tbCSVTextDelimiterRequiredFieldValidator);
        row.Cells.Add(cell);
        table.Rows.Add(row);
        row = new HtmlTableRow();
        cell = CreateHtmlTableCell();
        _lbCSVTextQualifier.Text = "CSV Text Qualifier: ";
        cell.Controls.Add(_lbCSVTextQualifier);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell(2);
        _tbCSVTextQualifier.ID = "tbCSVTextQualifier";
        _tbCSVTextQualifier.CssClass = "msrs-txtBox";
        _tbCSVTextQualifier.Style.Add("width", "100%");
        cell.Controls.Add(_tbCSVTextQualifier);
        row.Cells.Add(cell);
        cell = CreateHtmlTableCell();
        _tbCSVTextQualifierRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
        _tbCSVTextQualifierRequiredFieldValidator.ControlToValidate = "tbCSVTextQualifier";
        _tbCSVTextQualifierRequiredFieldValidator.Controls.Add(ErrorMessage("CSV Text Qualifier is required", true));
        cell.Controls.Add(_tbCSVTextQualifierRequiredFieldValidator);
        row.Cells.Add(cell);
        table.Rows.Add(row);
        Controls.Add(table);
    }

    public void SetConfiguration(string configuration)
    {
        CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
        try
        {
            Thread.CurrentThread.CurrentCulture = Microsoft.ReportingServices.Diagnostics.Localization.ClientPrimaryCulture;
            XmlDocument doc = new XmlDocument();
            Globals.SafeOpenXmlDocumentString(doc, configuration);
            if (doc.DocumentElement.Name == "RSSecureFTPDeliveryConfiguration")
            {
                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    string str;
                    if (((str = node.Name) != null) && (str == "DefaultRenderingExtension"))
                    {
                        _defaultRenderingExtension = node.InnerText;
                    }
                }
            }
        }
        catch (Exception exception)
        {
            throw new Microsoft.ReportingServices.Diagnostics.Utilities.ServerConfigurationErrorException(exception, null);
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = currentCulture;
        }
    }

    public bool Validate()
    {
        return Page.IsValid;
    }

    // Properties
    public string Description
    {
        get
        {
            return string.Format("Deliver report via FTP to {0}", _tbFTPUrl.Text);
        }
    }

    public bool IsPrivilegedUser
    {
        set
        {
            _isPriviledgedUser = value;
        }
    }

    public string LocalizedName
    {
        get
        {
            return "Secure FTP Delivery";
        }
    }

    public IDeliveryReportServerInformation ReportServerInformation
    {
        get
        {
            return _reportServerInformation;
        }
        set
        {
            _reportServerInformation = value;
        }
    }

    public Setting[] UserData
    {
        get
        {
            SubscriptionData subscriptionData = new SubscriptionData();
            subscriptionData.FTPUrl = _tbFTPUrl.Text;
            subscriptionData.FTPFolder = _tbFTPFolder.Text;
            subscriptionData.FTPUsername = _tbFTPUsername.Text;
            subscriptionData.FTPPassword = _tbFTPPassword.Text;
            subscriptionData.FTPPort = _tbFTPPort.Text;
            subscriptionData.IsDeliveryEncrypted = _cbIsDeliveryEncrypted.Checked.ToString();
            subscriptionData.PGPPublicKeyUID = _tbPGPPublicKeyUID.Text;
            subscriptionData.FileNameFormat = _tbFileNameFormat.Text;
            subscriptionData.CSVTextQualifier = _tbCSVTextQualifier.Text;
            subscriptionData.CSVTextDelimiter = _tbCSVTextDelimiter.Text;
            return subscriptionData.ToSettingArray();
        }
        set
        {
            SubscriptionData subscriptionData = new SubscriptionData();
            subscriptionData.FromSettings(value);
            _tbFTPUrl.Text = subscriptionData.FTPUrl;
            _tbFTPFolder.Text = subscriptionData.FTPFolder;
            _tbFTPUsername.Text = subscriptionData.FTPUsername;
            _tbFTPPassword.Text = subscriptionData.FTPPassword;
            _tbFTPPort.Text = subscriptionData.FTPPort;
            _cbIsDeliveryEncrypted.Checked = Convert.ToBoolean(subscriptionData.IsDeliveryEncrypted);
            _tbPGPPublicKeyUID.Text = subscriptionData.PGPPublicKeyUID;
            _tbFileNameFormat.Text = subscriptionData.FileNameFormat;
            _tbCSVTextDelimiter.Text = subscriptionData.CSVTextDelimiter;
            _tbCSVTextQualifier.Text = subscriptionData.CSVTextQualifier;
        }
    }
}

 
}
